package com.uchedao.seller.ui.carlist.fragment;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.uchedao.seller.R;
import com.uchedao.seller.ui.carlist.BaseFragForBaidu;
import com.uchedao.seller.ui.carlist.adapter.ModelAdapter;
import com.uchedao.seller.ui.carlist.db.CarDbHelper;
import com.uchedao.seller.ui.carlist.entity.ModelEntity;

import java.util.ArrayList;

/**
 * Created by ZhangZhanghua on 2015/9/14 0014.
 */
public class FragmentModel extends BaseFragForBaidu {
    private ListView modelListView;
    private ArrayList<ModelEntity> modelList;
    private ModelAdapter adapter;
    private int brandId;
    private int modelId;
    private int seriesId;
    private IModelClickListener listener;
    private String modelName;

    @Override
    protected String getActivityTag() {
        return "FragmentModel";
    }

    @Override
    public int getViewLayoutId() {
        return R.layout.fragment_model;
    }

    @Override
    public void initView() {
        modelListView = (ListView) findViewById(R.id.model);
    }

    @Override
    public void initData() {
        seriesId = getArguments().getInt("carSeriesId");
        modelList = CarDbHelper.getCarModel(getActivity(), seriesId);
        if (modelList.size() == 0) {
            listener.onModelClick(0, "0");
            getActivity().finish();
        }
        adapter = new ModelAdapter(getActivity(), modelList);
        modelListView.setAdapter(adapter);
    }

    @Override
    public void initListener() {

        modelListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = ((ModelEntity) adapter.getItem(position)).getName();
                modelId = ((ModelEntity) adapter.getItem(position)).getId();
                String year = ((ModelEntity) adapter.getItem(position)).getModel_year() + " ";
                modelName = year + name;
                listener.onModelClick(modelId, modelName);
                getActivity().finish();
            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
            listener =(IModelClickListener)activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");
        }
    }

    public interface IModelClickListener {
        void onModelClick(int mId, String mName);
    }

    @Override
    public void onActivityResultFragment(int requestCode, int resultCode, Intent data) {

    }
}
